package com.nmsl.statistic.controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.github.pagehelper.Page;
import com.nmsl.common.core.controller.BaseController;
import com.nmsl.common.core.domain.AjaxResult;
import com.nmsl.common.core.domain.entity.SysDictData;
import com.nmsl.common.core.page.TableDataInfo;
import com.nmsl.framework.config.RedisConfig;
import com.nmsl.statistic.constants.Constants;
import com.nmsl.statistic.domain.*;
import com.nmsl.statistic.job.TestJob;
import com.nmsl.statistic.mapper.*;
import com.nmsl.statistic.utils.RedisUtil;
import com.nmsl.statistic.utils.TimeUtil;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.aggregations.metrics.AvgAggregationBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.unit.DataUnit;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.TimeUnit;
import com.fasterxml.jackson.annotation.JsonFormat;

@RequestMapping("/zhr")
@RestController
public class TeststatisController extends BaseController {

    @Autowired
    private RedisTemplate redisTemplate;

    @RequestMapping("/test/{key}")
    public void test1(HttpServletRequest request, HttpServletResponse response, @PathVariable String key) throws IOException {
//        redisTemplate.opsForZSet().add("CityInfo:Hangzhou06", "1", 101);
//        redisTemplate.opsForZSet().add("CityInfo:Hangzhou06", "2", 20);
//        redisTemplate.opsForZSet().add("CityInfo:Hangzhou06", "3", 10);
//        redisTemplate.opsForZSet().add("CityInfo:Hangzhou06", "4", 30);
//        redisTemplate.opsForZSet().add("CityInfo:Hangzhou06", "5", 999);
//        获取
//        Long max = redisTemplate.opsForZSet().size("CityInfo:Hangzhou06");
//        System.out.println(max);
//
////        Set set = redisTemplate.opsForZSet().range("CityInfo:Hangzhou06", 0, 4);
//        Set set = redisTemplate.opsForZSet().range("CityInfo:Hangzhou06", max-2, max-1);
//        set.forEach((value) -> {
//            System.out.println("value=" + value.toString());
//        });

//        for (int i = 0; i < 300; i++) {
//            RetailAtt retailAtt = new RetailAtt();
//            retailAtt.setRetailAttId(i+"");
//            retailAtt.setAttPrice(new BigDecimal(i));
//            retailAtt.setAttId(Long.getLong(i+""));
//            retailAtt.setRetailOrderId(Long.getLong(i+""));
//            redisTemplate.opsForZSet().add(Constants.medicinesRanking, retailAtt, i*11);
//        }
//        RedisPage redisPage = RedisUtil.getMedicinesRanking(redisTemplate, 2l, 5l,
//                Constants.medicinesRanking,RetailAtt.class);
//        System.out.println(redisPage.getData());

        RedisUtil.updataMedicinesRanking(redisTemplate,key,"药品id",1);
//        RetailAttId  JSON.toJSONString


//        Long max = redisTemplate.opsForZSet().size(Constants.medicinesRanking);
//        Set set = redisTemplate.opsForZSet().range(Constants.medicinesRanking, max - 2, max - 1);
//        System.out.println(max);
//        System.out.println(set);
//        set.forEach((value) -> {
//            System.out.println(value);
//            RetailAtt  retailAtt6  = JSONObject.parseObject(JSON.toJSONString(value), RetailAtt.class);
//            System.out.println("value=" + retailAtt6.toString());
//        });
    }
    @Autowired
    McslfPatentMapper mcslfPatentMapper;
    @RequestMapping("/pentnt")
    public AjaxResult getP(){
//        McslfPatient mcslfPatient = mcslfPatentMapper.selectByPatientId(10);
//        System.out.println(mcslfPatient.toString());
        List<McslfPatient> mcslfPatient1 = mcslfPatentMapper.selectByPatientName("AAA");
        System.out.println(mcslfPatient1);
        return AjaxResult.success(mcslfPatient1);
    }
//    OperInfoMapper
@Autowired
OperInfoMapper operInfoMapper;
    @RequestMapping("/oper")
    public AjaxResult getOper(){
        List<OperInfo> list = operInfoMapper.selectAll();
        System.out.println(list);
        return AjaxResult.success(list);
    }
//    测试返回订单相关药品列表


    @Autowired
    TollManageMapper tollManageMapper;

    @Autowired
    RetailMedicineMapper retailMedicineMapper;

    @Autowired
    GetmedicInfoMapper getmedicInfoMapper;
    @Autowired
    TestJob testJob;
    @RequestMapping("/SAMBOID")
    public void selecetByRetailOrderId() throws ParseException {
//            redis 自旋锁
//        Boolean flag  = true;
//        while (flag){
//
//        }

    }

}
